home *** CD-ROM | disk | FTP | other *** search
/ Treccani Italiana Di Scienze Lettere Ed Arti / [Enciclopedia] Treccani Italiana di scienze lettere ed arti.iso / pc / data / xxi_appendice_dvd.swf / scripts / __Packages / StringUtils.as < prev   
Text File  |  2007-11-08  |  14KB  |  432 lines

  1. class StringUtils
  2. {
  3.    static var BACKSLASH = String.fromCharCode(92);
  4.    static var APICE = String.fromCharCode(39);
  5.    static var DOPPIOAPICE = String.fromCharCode(34);
  6.    static var TABSPACE = "    ";
  7.    static var IMAGE_URL = "";
  8.    static var USE_SUB_SUP_FONT = false;
  9.    static var mImageCounter = 0;
  10.    function StringUtils()
  11.    {
  12.    }
  13.    static function addSlashes(string)
  14.    {
  15.       var _loc3_ = "";
  16.       var _loc1_ = 0;
  17.       while(_loc1_ < string.length)
  18.       {
  19.          if(string.charAt(_loc1_) == StringUtils.BACKSLASH)
  20.          {
  21.             _loc3_ += StringUtils.BACKSLASH + StringUtils.BACKSLASH;
  22.          }
  23.          else if(string.charAt(_loc1_) == StringUtils.APICE)
  24.          {
  25.             _loc3_ += StringUtils.BACKSLASH + StringUtils.APICE;
  26.          }
  27.          else if(string.charAt(_loc1_) == StringUtils.DOPPIOAPICE)
  28.          {
  29.             _loc3_ += StringUtils.BACKSLASH + StringUtils.DOPPIOAPICE;
  30.          }
  31.          else
  32.          {
  33.             _loc3_ += string.charAt(_loc1_);
  34.          }
  35.          _loc1_ = _loc1_ + 1;
  36.       }
  37.       return _loc3_;
  38.    }
  39.    static function stripTrailingSpaces(string)
  40.    {
  41.       string;
  42.       string = StringUtils.replace(string,"\r","");
  43.       string = StringUtils.replace(string,"\n","");
  44.       string = StringUtils.replace(string,"\t","");
  45.       string = StringUtils.trim(string);
  46.       return string;
  47.    }
  48.    static function replace(s, arg_search, arg_replace)
  49.    {
  50.       StringUtils.string = s;
  51.       var _loc3_ = undefined;
  52.       var _loc1_ = "";
  53.       var _loc4_ = "";
  54.       var _loc5_ = "";
  55.       if(arg_search.length == 1)
  56.       {
  57.          return StringUtils.string.split(arg_search).join(arg_replace);
  58.       }
  59.       _loc3_ = StringUtils.string.indexOf(arg_search);
  60.       if(_loc3_ == -1)
  61.       {
  62.          return StringUtils.string;
  63.       }
  64.       _loc1_ = StringUtils.string;
  65.       do
  66.       {
  67.          _loc3_ = _loc1_.indexOf(arg_search);
  68.          _loc4_ = _loc1_.substring(0,_loc3_);
  69.          _loc1_ = _loc1_.substring(_loc3_ + arg_search.length);
  70.          _loc5_ += _loc4_ + arg_replace;
  71.       }
  72.       while(_loc1_.indexOf(arg_search) != -1);
  73.       
  74.       _loc5_ += _loc1_;
  75.       return _loc5_;
  76.    }
  77.    static function trim(s)
  78.    {
  79.       StringUtils.string = s;
  80.       var _loc1_ = undefined;
  81.       var _loc3_ = undefined;
  82.       var _loc2_ = undefined;
  83.       _loc3_ = StringUtils.string.length;
  84.       _loc1_ = 0;
  85.       while(StringUtils.string.charAt(_loc1_) == " ")
  86.       {
  87.          _loc1_ = _loc1_ + 1;
  88.       }
  89.       if(_loc1_)
  90.       {
  91.          StringUtils.string = StringUtils.string.substr(_loc1_ + 1,_loc3_);
  92.          if(_loc1_ == _loc3_)
  93.          {
  94.             return StringUtils.string;
  95.          }
  96.       }
  97.       _loc2_ = StringUtils.string.length - 1;
  98.       while(StringUtils.string.charAt(_loc2_) == " ")
  99.       {
  100.          _loc2_ = _loc2_ - 1;
  101.       }
  102.       StringUtils.string = StringUtils.string.substr(1,_loc2_ + 1);
  103.       return StringUtils.string;
  104.    }
  105.    static function trimWhiteAndCr(s)
  106.    {
  107.       StringUtils.string = s;
  108.       var _loc1_ = undefined;
  109.       var _loc3_ = undefined;
  110.       var _loc2_ = undefined;
  111.       _loc3_ = StringUtils.string.length;
  112.       _loc1_ = 0;
  113.       while(StringUtils.string.charAt(_loc1_) == " " || StringUtils.string.charCodeAt(_loc1_) == 13 || StringUtils.string.charCodeAt(_loc1_) == 10)
  114.       {
  115.          _loc1_ = _loc1_ + 1;
  116.       }
  117.       if(_loc1_)
  118.       {
  119.          StringUtils.string = StringUtils.string.substr(_loc1_ + 1,_loc3_);
  120.          if(_loc1_ == _loc3_)
  121.          {
  122.             return StringUtils.string;
  123.          }
  124.       }
  125.       _loc2_ = StringUtils.string.length - 1;
  126.       while(StringUtils.string.charAt(_loc2_) == " " || StringUtils.string.charCodeAt(_loc1_) == 13 || StringUtils.string.charCodeAt(_loc1_) == 10)
  127.       {
  128.          _loc2_ = _loc2_ - 1;
  129.       }
  130.       StringUtils.string = StringUtils.string.substr(1,_loc2_ + 1);
  131.       return StringUtils.string;
  132.    }
  133.    static function formatUrl(url)
  134.    {
  135.       if(url.indexOf("ftp://") != -1 || url.indexOf("news://") != -1 || url.indexOf("https://") != -1)
  136.       {
  137.          return url;
  138.       }
  139.       if(url.indexOf("http://") == -1 && url.indexOf("www") == -1)
  140.       {
  141.          return "http://www." + url;
  142.       }
  143.       if(url.indexOf("http://") != -1 && url.indexOf("www") == -1)
  144.       {
  145.          return StringUtils.replace(url,"http://","http://www.");
  146.       }
  147.       if(url.indexOf("http://") == -1)
  148.       {
  149.          return "http://" + url;
  150.       }
  151.    }
  152.    static function HtmlFunction(inLabel)
  153.    {
  154.       var _loc1_ = inLabel;
  155.       StringUtils.mImageCounter = 0;
  156.       var _loc3_ = _loc1_.indexOf("<sup>") >= 0;
  157.       var _loc2_ = _loc1_.indexOf("<sub>") >= 0;
  158.       var _loc4_ = _loc1_.indexOf("<img ") >= 0;
  159.       if(_loc3_ || _loc2_ || _loc4_)
  160.       {
  161.          _loc1_ = StringUtils.subSupImgFromHTML(_loc1_,_loc3_,_loc2_,_loc4_);
  162.          if(_loc3_ && _loc1_.indexOf("#A|") >= 0)
  163.          {
  164.             _loc1_ = _loc1_.split("#A|").join("<span class=\"s_Superscript\">");
  165.             _loc1_ = _loc1_.split("|A#").join("</span>");
  166.          }
  167.          if(_loc2_ && _loc1_.indexOf("#P|") >= 0)
  168.          {
  169.             _loc1_ = _loc1_.split("#P|").join("<span class=\"s_Subscript\">");
  170.             _loc1_ = _loc1_.split("|P#").join("</span>");
  171.          }
  172.       }
  173.       if(StringUtils.USE_SUB_SUP_FONT)
  174.       {
  175.          _loc1_ = _loc1_.split("<b>").join("<span class=\"s_Bold\">");
  176.          _loc1_ = _loc1_.split("</b>").join("</span>");
  177.       }
  178.       return _loc1_;
  179.    }
  180.    static function subSupImgFromHTML(origHTML, haveSup, haveSub, haveImg)
  181.    {
  182.       var _loc1_ = new XML();
  183.       var _loc2_ = undefined;
  184.       _loc1_.parseXML(origHTML);
  185.       if(haveSup || haveSub)
  186.       {
  187.          StringUtils.switchChars(_loc1_);
  188.       }
  189.       if(StringUtils.IMAGE_URL.length > 0 && haveImg)
  190.       {
  191.          StringUtils.handleEmbeddedImage(_loc1_);
  192.       }
  193.       _loc2_ = _loc1_.toString();
  194.       return _loc2_;
  195.    }
  196.    static function subSupFromHTML(origHTML)
  197.    {
  198.       var _loc2_ = new XML();
  199.       var _loc1_ = "";
  200.       _loc2_.parseXML(origHTML);
  201.       StringUtils.switchChars(_loc2_);
  202.       _loc1_ = _loc2_.toString();
  203.       if(_loc1_.indexOf("#A|") >= 0)
  204.       {
  205.          _loc1_ = _loc1_.split("#A|").join("<span class=\"s_Superscript\">");
  206.          _loc1_ = _loc1_.split("|A#").join("</span>");
  207.       }
  208.       if(_loc1_.indexOf("#P|") >= 0)
  209.       {
  210.          _loc1_ = _loc1_.split("#P|").join("<span class=\"s_Subscript\">");
  211.          _loc1_ = _loc1_.split("|P#").join("</span>");
  212.       }
  213.       if(StringUtils.USE_SUB_SUP_FONT)
  214.       {
  215.          _loc1_ = _loc1_.split("<b>").join("<span class=\"s_Bold\">");
  216.          _loc1_ = _loc1_.split("</b>").join("</span>");
  217.       }
  218.       return _loc1_;
  219.    }
  220.    static function handleEmbeddedImage(node)
  221.    {
  222.       if(node.nodeName != null)
  223.       {
  224.          if(node.nodeName == "img")
  225.          {
  226.             StringUtils.mImageCounter = StringUtils.mImageCounter + 1;
  227.             var _loc4_ = node.attributes.src;
  228.             var _loc3_ = _loc4_.split("#");
  229.             node.attributes.src = StringUtils.IMAGE_URL + _loc3_[0];
  230.             node.attributes.width = parseInt(_loc3_[1]);
  231.             node.attributes.height = parseInt(_loc3_[2]);
  232.          }
  233.       }
  234.       var _loc1_ = node.firstChild;
  235.       while(_loc1_ != null)
  236.       {
  237.          StringUtils.handleEmbeddedImage(_loc1_);
  238.          _loc1_ = _loc1_.nextSibling;
  239.       }
  240.    }
  241.    static function switchChars(node)
  242.    {
  243.       var _loc2_ = "";
  244.       if(node.nodeName != null)
  245.       {
  246.          if(node.nodeName == "sup")
  247.          {
  248.             var _loc1_ = "";
  249.             if(StringUtils.USE_SUB_SUP_FONT)
  250.             {
  251.                _loc1_ = "#A|" + node.firstChild.nodeValue + "|A#";
  252.             }
  253.             else
  254.             {
  255.                var _loc7_ = node.firstChild.nodeValue.length;
  256.                var _loc9_ = node.firstChild.nodeValue;
  257.                var _loc4_ = 0;
  258.                while(_loc4_ < _loc7_)
  259.                {
  260.                   _loc2_ = _loc9_.charAt(_loc4_);
  261.                   switch(_loc2_)
  262.                   {
  263.                      case "0":
  264.                         _loc1_ += "Γü░";
  265.                         break;
  266.                      case "1":
  267.                         _loc1_ += "┬╣";
  268.                         break;
  269.                      case "2":
  270.                         _loc1_ += "┬▓";
  271.                         break;
  272.                      case "3":
  273.                         _loc1_ += "┬│";
  274.                         break;
  275.                      case "4":
  276.                         _loc1_ += "Γü┤";
  277.                         break;
  278.                      case "5":
  279.                         _loc1_ += "Γü╡";
  280.                         break;
  281.                      case "6":
  282.                         _loc1_ += "Γü╢";
  283.                         break;
  284.                      case "7":
  285.                         _loc1_ += "Γü╖";
  286.                         break;
  287.                      case "8":
  288.                         _loc1_ += "Γü╕";
  289.                         break;
  290.                      case "9":
  291.                         _loc1_ += "Γü╣";
  292.                         break;
  293.                      case "+":
  294.                         _loc1_ += "Γü║";
  295.                         break;
  296.                      case "=":
  297.                         _loc1_ += "Γü╝";
  298.                         break;
  299.                      case "(":
  300.                         _loc1_ += "Γü╜";
  301.                         break;
  302.                      case ")":
  303.                         _loc1_ += "Γü╛";
  304.                         break;
  305.                      case "n":
  306.                         _loc1_ += "Γü┐";
  307.                         break;
  308.                      case "-":
  309.                      case String.fromCharCode(8722):
  310.                         _loc1_ += "Γü╗";
  311.                         break;
  312.                      default:
  313.                         _loc1_ += "#A|" + _loc2_ + "|A#";
  314.                         break;
  315.                   }
  316.                   _loc4_ = _loc4_ + 1;
  317.                }
  318.             }
  319.             node.firstChild.nodeValue = _loc1_;
  320.          }
  321.          else if(node.nodeName == "sub")
  322.          {
  323.             _loc1_ = "";
  324.             if(StringUtils.USE_SUB_SUP_FONT)
  325.             {
  326.                _loc1_ = "#P|" + node.firstChild.nodeValue + "|P#";
  327.             }
  328.             else
  329.             {
  330.                var _loc6_ = node.firstChild.nodeValue.length;
  331.                var _loc8_ = node.firstChild.nodeValue;
  332.                _loc4_ = 0;
  333.                while(_loc4_ < _loc6_)
  334.                {
  335.                   _loc2_ = _loc8_.charAt(_loc4_);
  336.                   switch(_loc2_)
  337.                   {
  338.                      case "0":
  339.                         _loc1_ += "ΓéÇ";
  340.                         break;
  341.                      case "1":
  342.                         _loc1_ += "Γéü";
  343.                         break;
  344.                      case "2":
  345.                         _loc1_ += "Γéé";
  346.                         break;
  347.                      case "3":
  348.                         _loc1_ += "Γéâ";
  349.                         break;
  350.                      case "4":
  351.                         _loc1_ += "Γéä";
  352.                         break;
  353.                      case "5":
  354.                         _loc1_ += "Γéà";
  355.                         break;
  356.                      case "6":
  357.                         _loc1_ += "Γéå";
  358.                         break;
  359.                      case "7":
  360.                         _loc1_ += "Γéç";
  361.                         break;
  362.                      case "8":
  363.                         _loc1_ += "Γéê";
  364.                         break;
  365.                      case "9":
  366.                         _loc1_ += "Γéë";
  367.                         break;
  368.                      case "+":
  369.                         _loc1_ += "Γéè";
  370.                         break;
  371.                      case "-":
  372.                         _loc1_ += "Γéï";
  373.                         break;
  374.                      case "=":
  375.                         _loc1_ += "Γéî";
  376.                         break;
  377.                      case "(":
  378.                         _loc1_ += "Γéì";
  379.                         break;
  380.                      case ")":
  381.                         _loc1_ += "ΓéÄ";
  382.                         break;
  383.                      default:
  384.                         _loc1_ += "#P|" + _loc2_ + "|P#";
  385.                   }
  386.                   _loc4_ = _loc4_ + 1;
  387.                }
  388.             }
  389.             node.firstChild.nodeValue = _loc1_;
  390.          }
  391.       }
  392.       var _loc3_ = node.firstChild;
  393.       while(_loc3_ != null)
  394.       {
  395.          StringUtils.switchChars(_loc3_);
  396.          _loc3_ = _loc3_.nextSibling;
  397.       }
  398.    }
  399.    static function TabToSpace(inTabNumber)
  400.    {
  401.       var _loc2_ = "";
  402.       var _loc1_ = 0;
  403.       while(_loc1_ < inTabNumber)
  404.       {
  405.          _loc2_ += StringUtils.TABSPACE;
  406.          _loc1_ = _loc1_ + 1;
  407.       }
  408.       return _loc2_;
  409.    }
  410.    static function HtmlToText(inHtml)
  411.    {
  412.       if(!_global.gHtmlRenderer)
  413.       {
  414.          _global.gHtmlRenderer = _root.createTextField("txt_globalhtmlRenderer",1,0,0,100,200);
  415.          _global.gHtmlRenderer.html = true;
  416.          _global.gHtmlRenderer._visible = false;
  417.          _global.gHtmlRenderer.multiline = true;
  418.       }
  419.       _global.gHtmlRenderer.htmlText = inHtml;
  420.       return _global.gHtmlRenderer.text;
  421.    }
  422.    static function StripExt(inFilename)
  423.    {
  424.       var _loc1_ = inFilename.lastIndexOf(".");
  425.       if(_loc1_ >= 0)
  426.       {
  427.          return inFilename.substring(0,_loc1_);
  428.       }
  429.       return inFilename;
  430.    }
  431. }
  432.